#ifndef AUTORUNPROCESS_H
#define AUTORUNPROCESS_H

#include <QObject>
#include <QTimer>
#include <QDebug>
#include <QQueue>
#include <QTime>
#include "quicktool.h"

class AutoRunProcess : public QObject {
    Q_OBJECT
public:
    explicit AutoRunProcess(QObject *parent = nullptr);

    const int AntiShakeDelay = 50 ;
    void plcStopEmit();

    OR_QML_STATE_PROPERTY(bool, isRunning, false)
    OR_QML_STATE_PROPERTY(bool, sudentStop, false)


    //updateisRunning(true);

    void detectContinous();

signals:
    void sudentStopContinue();

    void processingCompleted();
    void StopTheMachinesignals();

    void shortageGriggerSign();

    void inplaceSign(); //  updatesudentStop(true);
    void inplaceSlidingStepSign();

    void logosizeChangeSign();
    void arriveBatchGriggerSign();

public slots:
    void setThreadPriority();

    void startProcess();
    void stopProcess();
    void continueProcess();

    void onIO1SignalReceived();
    void onIO2SignalReceived();
    void oninplaceGriggerReceived();
    void shortageGriggerSlot();

    void startPLC();
    void stopPLC();
    void RealTimestopPLC();

private slots:
    void moveDistanceSlot(float distance);

    void captureImgMode();
    void determineRemoveWaste();

    void StopTheMachineSlots();

private:
    bool inplaceStopLunch = false;
    int skipStepOver = 0;
    float moveDistanceMax = 9999;
    float moveDistanceMin = -9999;
    bool skipOnce = false;

    void StopTheMachine();
    bool isInPlayce = true;
    bool plcStop_ = false;
    int receivedIO1 = 0;

    QVector<double> FabricSpacingList ;
    QVector<double> moveDistanceList ;
    QTimer *IO1debounceTimer;
    QTimer *IO2debounceTimer;
    void detectInplayce();
    void calculateAdjustedRange(QVector<double> &moveDistanceList);
    void recordCallInterval();


    QQueue<int> intervalTimes;  // 存储每次调用到上一次调用的间隔时间（毫秒）
    QDateTime lastCallTime;     // 上次调用的时间
    int captureCount = 0;       // 当前分钟内的调用次数
    QTimer* frequencyTimer;     // 用于每分钟统计一次频率
};
#endif // AUTORUNPROCESS_H
